package beautiful.butterfly.drds.data_exchange.util;

import java.lang.reflect.Constructor;

public final class Classs
{

    /**
     * 通过反射构造类对象
     *
     * @param className 反射的类名称
     * @param aClass    反射类的类型Class对象
     * @param args      构造参数
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <T> T newInstance(String className, Class<T> aClass, Object... args)
    {
        try
        {
            Constructor constructor = Class.forName(className).getConstructor(Classs.getClassType(args));
            return (T) constructor.newInstance(args);
        } catch (Exception e)
        {
            throw new IllegalArgumentException(e);
        }
    }

    private static Class<?>[] getClassType(Object[] args)
    {
        Class<?>[] classes = new Class<?>[args.length];

        for (int i = 0, length = args.length; i < length; i++)
        {
            classes[i] = args[i].getClass();
        }

        return classes;
    }

}
